package com.shuwen.gcdj.controller.power;

import com.shuwen.gcdj.bean.PagingReq;
import com.shuwen.gcdj.bean.power.request.MenuSettingReq;
import com.shuwen.gcdj.bean.power.response.PowerParentResp;
import com.shuwen.gcdj.bean.role.request.GetAllRoleReq;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.power.request.AddPowerReq;
import com.shuwen.gcdj.bean.power.request.GetAllPowerReq;
import com.shuwen.gcdj.bean.power.request.UpdatePowerReq;
import com.shuwen.gcdj.bean.power.response.PowerItemResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.common.util.paging.PagingUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.power.PowerApiService;
import com.shuwen.gcdj.service.power.PowerService;
import com.shuwen.gcdj.service.role.RoleService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - 权限码管理(可用)")
@RestController
@RequestMapping("/power")
@Slf4j
public class PowerController extends AuthController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private PowerService powerService;

    @Autowired
    private PowerApiService powerApiService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @ApiOperation(value = "分页获取所有权限码")
    @PostMapping(value = "/getAllList")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<PowerItemResp>> getAllList(HttpServletRequest request, @RequestBody PagingReq page) {
        GetAllPowerReq req = new GetAllPowerReq();
        InitUserDto initUserDto = initUser(request);
        if (initUserDto != null && initUserDto.getUserId() != null && roleService.isSupper(initUserDto.getRoleIds())) {
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL, Constants.IS_HIDDEN_YES));
        } else {
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL));
        }
        PagingUtil.withDefault(page);
        return CommonResult.success(CommonPage.restPage(powerService.getAllList(req,page)));

    }

    @ApiOperation(value = "获取所有权限菜单")
    @GetMapping(value = "/getAllParentList")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<PowerParentResp>> getAllParentList(HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        List<PowerParentResp> parentResps = powerService.getAllParentList();
        return CommonResult.success(parentResps);

    }

    @ApiOperation(value = "根据权限ID获取权限详情")
    @GetMapping(value = "/getbypowerid/{powerId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<PowerItemResp> getbyroleid(HttpServletRequest request, @PathVariable String powerId) {
        if(StringUtils.isEmpty(powerId)){
            return CommonResult.failed("powerId不可为空");
        }
        InitUserDto initUserDto = initUser(request);
        log.info("initUserDto = {}", initUserDto);
        GetAllRoleReq req = new GetAllRoleReq();
        req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL));
        if (initUserDto != null && initUserDto.getUserId() != null && roleService.isSupper(initUserDto.getRoleIds())) {
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL, Constants.IS_HIDDEN_YES));
        }

        return CommonResult.success(powerService.getBypowerId(req,powerId));

    }

    @ApiOperation(value = "添加权限码(可用)")
    @PostMapping(value = "/add")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> add(HttpServletRequest request, @Valid @RequestBody AddPowerReq req) {
        return CommonResult.success(powerService.createPower(req, initUser(request)));

    }

    @ApiOperation(value = "修改权限码(可用)")
    @PostMapping(value = "/update")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> update(HttpServletRequest request, @Valid @RequestBody UpdatePowerReq req) {
        powerService.updatePower(req, initUser(request));
        InitUserDto initUserDto = initUser(request);
        addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_UPDATE.getTypeNum(), LogModuleEnum.LOG_MODULE_POWER.getModuleNum(), "修改了权限",req);
        return CommonResult.success(new VoidResp());

    }

    /**
     * 删除权限
     *
     * @param powerCode String
     * @return CommonResult<VoidResp>
     */
    @ApiOperation(value = "批量删除权限码")
    @GetMapping(value = "/delete/{powerCode}")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> delete(HttpServletRequest request, @Valid @PathVariable String powerCode) {
        powerService.deletePower(powerCode, initUser(request));
        return CommonResult.success(new VoidResp());
    }

    /**
     * 同步菜单信息
     * @return
     */
    @ApiOperation(value = "同步菜单信息")
    @PostMapping(value = "/syncMenuSetting")
    @ResponseBody
//    @JWTAuth(apiCode = "USER_API_POWER_ADD")
//    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> syncMenuSetting(@Valid @RequestBody List<MenuSettingReq> req)
    {
        Boolean result = false;
        result = powerService.initSyncMenuTempalte(req);
        return CommonResult.success(result);
    }
}
